package com.klod.data.services

import com.klod.data.mapping.UserBean
import com.klod.data.repository.NewsRepository
import com.klod.data.repository.UserRepository
import com.klod.data.repository.UserRepositoryI
import org.koin.core.annotation.Single

@Single
class UserServices(
    private val userRepository: UserRepository
) :UserRepositoryI {
    override suspend fun findAll() = userRepository.findAll()

    override suspend fun findById(id: Int) = userRepository.findById(id)

    override suspend fun findByAccount(account: String) = userRepository.findByAccount(account)

    override suspend fun addUser(bean: UserBean) = userRepository.addUser(bean)

    override suspend fun deleteUser(id: Int) = userRepository.deleteUser(id)



    //检查登陆账户是否合法
    suspend fun checkLogin(bean: UserBean):Boolean{

        val user = findByAccount(bean.account)
        return if (user == null){
            false
        }else{
            user.account==bean.account && user.password == bean.password
        }

    }
    //注册帐号
    suspend fun register(bean: UserBean):Boolean{

        val user = findByAccount(bean.account)
        return if (user == null){
            addUser(bean) !=-1
        }else{
           false
        }

    }

}